package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CoReturnListService;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-30 13:31
 */

@RestController
@RequestMapping("customerReturnListGoods")
public class CostomerReturnListController {

    @Autowired
    private CoReturnListService coReturnListService;

    @Autowired
    private Gson gson;

    //http://localhost:8080/customerReturnListGoods/save?returnNumber=XT1605772786495
    //退货单保存
    @PostMapping("save")
    public ServiceVO save(@RequestParam("returnNumber") String returnNumber,
                          CustomerReturnList customerReturnList,
                          String customerReturnListGoodsStr,
                          String customerName,
                          HttpSession session){
        User currentUser = (User) session.getAttribute("currentUser");
        Integer userId = currentUser.getUserId();

        List<CustomerReturnListGoods> customerReturnListGoodsList = gson.fromJson(customerReturnListGoodsStr, new TypeToken<List<CustomerReturnListGoods>>() {}.getType());

        customerReturnList.setUserId(userId);
        customerReturnList.setReturnNumber(returnNumber);
        customerReturnList.setCustomerName(customerName);

        return coReturnListService.coReturnListGoodsListSave(customerReturnList,customerReturnListGoodsList);
    }


    //客户退货单查询
    //http://localhost:8080/customerReturnListGoods/list
    @PostMapping("list")
    public Map<String,Object> list(@Param("returnNumber") String returnNumber,
                                   @Param("customerId") Integer customerId,
                                   @Param("state") Integer state,
                                   @Param("sTime") String sTime,
                                   @Param("eTime") String eTime){

        return coReturnListService.getCoReturnSearchList(returnNumber,customerId,state,sTime,eTime);
    }


    //退货单商品信息
    //http://localhost:8080/customerReturnListGoods/goodsList
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(@Param("customerReturnListId") Integer customerReturnListId){

        return coReturnListService.getCoReturnGoodsList(customerReturnListId);
    }


    //http://localhost:8080/customerReturnListGoods/delete
    //删除退货单
    @PostMapping("delete")
    public ServiceVO delete(@Param("customerReturnListId") Integer customerReturnListId){
        return coReturnListService.deleteCoReturnListGoods(customerReturnListId);
    }

    //http://localhost:8080/customerReturnListGoods/updateState
    //修改销售推货单
    @PostMapping("updateState")
    public ServiceVO updateState(@Param("customerReturnListId") Integer customerReturnListId){
        return coReturnListService.updateCoReturnListGoods(customerReturnListId);
    }
}
